/* GUI CONVERTED from reoptr07.p (converter v1.76) Sun Aug 25 20:59:41 2002 */
/* reoptr07.p - SETUP OPERATION TRANSACTION BY PART (REPETITIVE)              */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.9.3.5 $                                                               */
/*V8:ConvertMode=Maintenance                                                  */
/* REVISION: 5.0      LAST MODIFIED: 05/15/89   BY: emb                       */
/* REVISION: 5.0      LAST MODIFIED: 09/17/90   BY: emb *B801*                */
/* REVISION: 6.0      LAST MODIFIED: 09/25/90   BY: SMM *D072*                */
/* REVISION: 6.0      LAST MODIFIED: 11/21/90   BY: emb *D223*                */
/* REVISION: 6.0      LAST MODIFIED: 03/22/91   BY: emb *D448*                */
/* REVISION: 6.0      LAST MODIFIED: 04/09/91   BY: WUG *D503*                */
/* REVISION: 6.0      LAST MODIFIED: 06/14/91   BY: emb *D705*                */
/* REVISION: 6.0      LAST MODIFIED: 08/09/91   BY: pma *D806*                */
/* REVISION: 7.0      LAST MODIFIED: 10/08/91   BY: pma *F003*                */
/* REVISION: 7.0      LAST MODIFIED: 03/10/92   BY: smm *F230*                */
/* REVISION: 7.0      LAST MODIFIED: 03/23/92   BY: pma *F089*                */
/* REVISION: 7.0      LAST MODIFIED: 03/29/92   BY: emb *F331*                */
/* REVISION: 7.0      LAST MODIFIED: 06/16/92   BY: emb *F620*                */
/* REVISION: 7.0      LAST MODIFIED: 06/17/92   BY: emb *F662*                */
/* REVISION: 7.0      LAST MODIFIED: 10/13/92   BY: emb *G184*                */
/* REVISION: 7.3      LAST MODIFIED: 02/10/93   BY: emb *G689*                */
/* REVISION: 7.3      LAST MODIFIED: 02/22/93   BY: emb *G760*                */
/* REVISION: 7.3      LAST MODIFIED: 03/30/93   BY: emb *G882*                */
/* REVISION: 7.4      LAST MODIFIED: 07/22/93   BY: pcd *H039*                */
/* REVISION: 7.4      LAST MODIFIED: 11/08/93   BY: ais *H205*                */
/* REVISION: 7.4      LAST MODIFIED: 01/12/94   BY: qzl *H276*                */
/* REVISION: 7.4      LAST MODIFIED: 08/15/94   BY: pxd *GL30*                */
/* REVISION: 8.5      LAST MODIFIED: 10/24/94   BY: mwd *J034*                */
/* REVISION: 7.3      LAST MODIFIED: 10/31/94   BY: WUG *GN76*                */
/* REVISION: 7.4      LAST MODIFIED: 12/29/94   BY: cpp *FT95*                */
/* REVISION: 7.3      LAST MODIFIED: 05/11/95   BY: ais *G0MM**/
/* REVISION: 8.5      LAST MODIFIED: 06/16/95   BY: rmh *J04R*                */
/* REVISION: 8.5      LAST MODIFIED: 02/14/96   BY: jym *G1N7*                */
/* REVISION: 8.5      LAST MODIFIED: 04/09/96   BY: jym *G1Q9*                */
/* REVISION: 8.5      LAST MODIFIED: 05/01/96   BY: jym *G1MN*                */
/* REVISION: 8.5      LAST MODIFIED: 05/30/96   BY: lal *J0PN*                */
/* REVISION: 8.6      LAST MODIFIED: 06/11/96   BY: ejh *K001*                */
/* REVISION: 8.6      LAST MODIFIED: 01/27/97   BY: *G2JV* Julie Milligan     */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane          */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan         */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 9.1      LAST MODIFIED: 10/25/99   BY: *N002* Bill Gates         */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane   */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00   BY: *N0KP* Mark Brown         */
/* Old ECO marker removed, but no ECO header exists *F0PN*                    */
/* Revision: 1.9.3.4     BY: Jean Miller       DATE: 05/21/02  ECO: *P05V*  */
/* $Revision: 1.9.3.5 $          BY: Vivek Gogte       DATE: 08/06/02  ECO: *N1QQ*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{mfdtitle.i "2+ "}

{gldydef.i new}
{gldynrm.i new}

define new shared variable gldetail like mfc_logical no-undo initial no.
define new shared variable gltrans like mfc_logical no-undo initial no.
define new shared variable wr_recno as recid.
define new shared variable wc_recno as recid.
define new shared variable op_recno as recid.

define new shared variable comp like ps_comp.
define new shared variable eff_date as date.
define new shared variable operation like ps_op.
define new shared variable wo_recid as recid.
define new shared variable wo_recno as recid.
define new shared variable ophist_recid as recid.
define new shared variable pay_rate like emp_pay_rate.
define new shared variable last_date like op_date.
define new shared variable shift like op_shift.
define new shared variable earn like op_earn.
define new shared variable site like rps_site no-undo.
define new shared variable prline like rps_line no-undo.
define new shared variable part like op_part no-undo.
define new shared variable op like ps_op no-undo.
define new shared variable bom_code as character format "x(18)" label "BOM".
define new shared variable routing  as character format "x(18)" label "Routing".
define new shared variable schedule_id like wo_lot.
define new shared variable opemp like op_emp.
define new shared variable input_total as decimal format "->>9.9<<"
   label "Input Total".
define new shared variable control_total like input_total
   label "Control Total".
define new shared variable h_wiplottrace_procs as handle no-undo.
define new shared variable h_wiplottrace_funcs as handle no-undo.

define variable start_setup as character format "99:99:XX" label "Start Time".
define variable start_run  like start_setup.
define variable stop_setup like start_setup label "Elapsed or Stop Time".
define variable stop_run like stop_setup.
define variable i as integer.
define variable j as integer.
define variable old_emp like op_emp.
define variable ptstatus like pt_status.
define variable msg-type as integer.
define variable ok like mfc_logical no-undo.
define variable prompt-routing like mfc_logical no-undo.
define variable cancel like mfc_logical no-undo.

define new shared frame a.
define new shared frame b.

{wlfnc.i} /*FUNCTION FORWARD DECLARATIONS*/

if is_wiplottrace_enabled() then do:
   {gprunmo.i &program=""wlpl.p"" &module="AWT"
      &persistent="""persistent set h_wiplottrace_procs"""}
   {gprunmo.i &program=""wlfl.p"" &module="AWT"
      &persistent="""persistent set h_wiplottrace_funcs"""}
end.

{gpglefv.i}

last_date = today.


/*GUI preprocessor Frame A define */
&SCOPED-DEFINE PP_FRAME_NAME A

FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1 COLUMN 3 NO-LABEL VIEW-AS TEXT SIZE-PIXELS 1 BY 1
 SKIP(.1)  /*GUI*/
op_emp        colon 16
   ad_name       no-label no-attr-space
   input_total   colon 69
   skip
   last_date     colon 16
   shift
   site
   control_total colon 69
 SKIP(.4)  /*GUI*/
with frame a side-labels width 80 attr-space NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-a-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame a = F-a-title.
 RECT-FRAME-LABEL:HIDDEN in frame a = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame a =
  FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME a = FRAME a:WIDTH-CHARS - .5.  /*GUI*/

/*GUI preprocessor Frame A undefine */
&UNDEFINE PP_FRAME_NAME



/* SET EXTERNAL LABELS */
setFrameLabels(frame a:handle).

FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1 COLUMN 3 NO-LABEL VIEW-AS TEXT SIZE-PIXELS 1 BY 1
 SKIP(.1)  /*GUI*/
part           colon 22 pt_desc1       no-label
   op             colon 22 ro_desc        no-label
   prline         colon 22 ln_desc        no-label
   routing        colon 22 bom_code
   op_wkctr       colon 22 op_mch wc_desc no-label no-attr-space
   op_dept        colon 22 dpt_desc       no-label no-attr-space
   start_run      colon 65
   op_act_setup   colon 22
   stop_run       colon 65
   op_earn        colon 22 ea_desc no-label no-attr-space
   op_trnbr       colon 22 format ">>>>>>>9"
   op_wo_lot      colon 41
 SKIP(.4)  /*GUI*/
with frame b side-labels width 80 attr-space NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-b-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame b = F-b-title.
 RECT-FRAME-LABEL:HIDDEN in frame b = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame b =
  FRAME b:HEIGHT-PIXELS - RECT-FRAME:Y in frame b - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME b = FRAME b:WIDTH-CHARS - .5.  /*GUI*/


/* SET EXTERNAL LABELS */
setFrameLabels(frame b:handle).

/* DISPLAY */
view frame a.
view frame b.

find mfc_ctrl where mfc_field = "rpc_using_new" no-lock no-error.

if available mfc_ctrl and mfc_logical = true then do:
   /* Use of this function not allowed because new Repetitive enabled */
   {pxmsg.i &MSGNUM=5122 &ERRORLEVEL=3}
   message.
   message.
   undo, leave.
end.

find first ea_mstr no-lock where ea_type = "1" no-error.
if available ea_mstr then earn = ea_earn.

mainloop:
repeat:
/*GUI*/ if global-beam-me-up then undo, leave.


   do with frame a:

      ststatus = stline[1].
      status input ststatus.

      prompt-for
         op_emp
      with frame a editing:

         /* FIND NEXT/PREVIOUS RECORD */
         {mfnp.i emp_mstr op_emp emp_addr op_emp emp_addr emp_addr}

         if recno <> ? then do:
            display
               emp_addr @ op_emp
               emp_fname + " " + emp_lname @ ad_name.
         end.
      end.

      /*Assign shared variable so subprocedure reoptr7b can re-find */
      opemp = input op_emp.

      find emp_mstr where emp_addr = input op_emp no-lock no-error.

      if not available emp_mstr then do:
         {pxmsg.i &MSGNUM=520 &ERRORLEVEL=3}
         /* Employee number does not exist */
         undo, retry.
      end.

      if lookup(emp_status,"AC,PT") = 0 then do:
         {pxmsg.i &MSGNUM=4027 &ERRORLEVEL=3}
         /* Employee not on active status */
         undo,retry.
      end.

      display
         emp_addr @ op_emp
         emp_fname + " " + emp_lname @ ad_name.

      if old_emp <> ? and old_emp <> input op_emp then
      assign
         control_total = 0
         input_total = 0.

      display
         last_date
         shift
         control_total
         input_total.

      set1:
      do on error undo, retry:
/*GUI*/ if global-beam-me-up then undo, leave.


         set
            last_date
            shift
            site
            control_total
         editing:

            if frame-field = "site" then do:
               /* FIND/NEXT PREVIOUS*/
               {mfnp.i si_mstr site si_site site si_site si_site}
               if recno <> ? then
                  display si_site @ site.
            end.
            else do:
               readkey.
               apply lastkey.
            end.

         end. /* editing */

         find si_mstr no-lock where si_site = site no-error.
         if not available si_mstr then do:
            /* Site does not exist */
            {pxmsg.i &MSGNUM=708 &ERRORLEVEL=3}
            next-prompt site.
            undo, retry.
         end.

         {gprun.i ""gpsiver.p""
            "(input si_site, input recid(si_mstr), output return_int)"}
/*GUI*/ if global-beam-me-up then undo, leave.

         if return_int = 0 then do:
            /* User does not have access to this site */
            {pxmsg.i &MSGNUM=725 &ERRORLEVEL=3}
            next-prompt site.
            undo, retry.
         end.

         /* Check effective date*/
         {gpglef1.i
            &module = ""WO""
            &entity = si_entity
            &date = last_date
            &prompt = "last_date"
            &frame = "a"
            &loop = "set1"}

      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* set statement */

   end.

   old_emp = input op_emp.

   repeat-loop:
   repeat with frame b:
/*GUI*/ if global-beam-me-up then undo, leave.


      run update_next_previous (output cancel).
      if cancel then leave.

      find pt_mstr no-lock where pt_part = part no-error.
      if not available pt_mstr then do:
         {pxmsg.i &MSGNUM=16 &ERRORLEVEL=3}
         undo, retry.
      end.

      assign prline.

      find first ln_mstr no-lock where ln_site = site no-error.

      if available ln_mstr or prline <> "" then do:

         find ln_mstr where ln_line = prline
                        and ln_site = site
         no-lock no-error.

         if not available ln_mstr then do:
            /* Site Production line does not exist */
            {pxmsg.i &MSGNUM=8526 &ERRORLEVEL=3}
            next-prompt prline.
            display "" @ ln_desc.
            undo,retry.
         end.

         display ln_desc.

         find first lnd_det where lnd_line = prline
            and lnd_site = site
            and lnd_part = part
            and (lnd_start <= today or lnd_start = ?)
         no-lock no-error.

         if not available lnd_det then do:
            /* Item not defined on this Production Line */
            {pxmsg.i &MSGNUM=8527 &ERRORLEVEL=3}
            next-prompt prline.
            undo, retry.
         end.

      end.
      else
         display "" @ ln_desc.

      /* Added section to locate proper schedule to update */
      {gprun.i ""reoptr1b.p""
         "(site, prline, part, op, last_date,
           output routing, output bom_code, output schedule_id)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      if keyfunction(lastkey) = "end-error" then undo, retry.

      if schedule_id = ? then do:
         msg-type = 3.
         /* Unexploded schedule with consumption period */
         {pxmsg.i &MSGNUM=325 &ERRORLEVEL=msg-type}
         next-prompt part.
         undo, retry.
      end.

      mainloop:
      do on error undo, retry:
/*GUI*/ if global-beam-me-up then undo, leave.


         update
            routing
            bom_code
         with frame b editing:

            if frame-field = "routing" then do:

               /* FIND NEXT/PREVIOUS RECORD */
               {mfnp05.i ptr_det ptr_part
                  "ptr_part = part and ptr_site = site"
                  ptr_routing "input routing"}

               if recno <> ? then do:
                  display ptr_routing @ routing.
                  recno = ?.
               end.
            end.

            else
            if frame-field = "bom_code" then do:

               /* FIND NEXT/PREVIOUS RECORD */
               {mfnp05.i ptr_det ptr_part
                  "ptr_part = part
                    and ptr_site = site
                    and ptr_routing = input routing"
                  ptr_bom_code "input bom_code"}

               if recno <> ? then do:
                  display ptr_bom_code @ bom_code.
                  recno = ?.
               end.
            end.

            else do:
               readkey.
               apply lastkey.
            end.

         end.

         /* Validate Work Order Routing and BOM Codes */
         {gprun.i ""wortbmv.p""
            "(input part,
              input site,
              input routing,
              input bom_code,
              input 3,
              output ok,
              output prompt-routing)"}
/*GUI*/ if global-beam-me-up then undo, leave.


         if not ok then do:
            if prompt-routing then
               next-prompt routing.
            else next-prompt bom_code.
            undo,retry.
         end.

         if routing = "" and
            not can-find (first ro_det where ro_routing = part)
         then do:
            next-prompt routing.
            /* ROUTING DOES NOT EXISTS */
            {pxmsg.i &MSGNUM=126 &ERRORLEVEL=3}
            undo, retry.
         end.

      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* mainloop */

      if schedule_id <> "" then do:

         find wo_mstr no-lock where wo_lot = schedule_id no-error.

         schedule_id = "".

         if available wo_mstr
            and (wo_bom_code = bom_code
             or (wo_bom_code = wo_part and bom_code = "")
             or (wo_bom_code = "" and bom_code = wo_part))
            and (wo_routing = routing
             or (wo_routing = wo_part and routing = "")
             or (wo_routing = "" and routing = wo_part))
         then
            schedule_id = wo_lot.

      end.

      find first wo_mstr
         where wo_type = "C"
           and wo_part = part
           and wo_site = site
           and wo_line = prline
           and (wo_routing = routing or (wo_routing = part and routing = ""))
           and (wo_bom_code = bom_code or (wo_bom_code = part and bom_code = ""))
      no-lock no-error.

      repeat:

         if not available wo_mstr then leave.
         if wo_status <> "C" then leave.

         find next wo_mstr
            where wo_type = "C"
              and wo_part = part
              and wo_site = site
              and wo_line = prline
              and (wo_routing = routing or (wo_routing = part and routing = ""))
              and (wo_bom_code = bom_code or (wo_bom_code = part and bom_code = ""))
         no-error.

      end.

      /*VALIDATE THE OPERATION */
      {reopval.i &part = part
         &routing = routing
         &op = op
         &date = last_date
         &prompt = op
         &frame = "b"
         &leave = ""no""
         &loop = "mainloop"}

      if not available wo_mstr then do:

         assign
            ptstatus = pt_status
            substring(ptstatus,9,1) = "#".

         if can-find(isd_det where isd_status = ptstatus
                               and isd_tr_type = "ADD-RE")
         then do:
            /* Restricted Procedure for item status code # */
            {pxmsg.i &MSGNUM=358 &ERRORLEVEL=3 &MSGARG1=pt_status}
            undo repeat-loop, retry repeat-loop.
         end.

         {gprun.i ""rewocr.p"" "(part,last_date)"}
/*GUI*/ if global-beam-me-up then undo, leave.


         find wo_mstr no-lock where recid(wo_mstr) = wo_recno.

      end.

      do transaction:

         find wr_route where wr_lot = wo_lot
                         and wr_op = op
         no-lock no-error.

         if not available wr_route then do:
            /* CREATE wr_route from ro_det for part and operation */
            create wr_route.
            assign
               wr_nbr = wo_nbr
               wr_lot = wo_lot
               wr_part = wo_part
               wr_op = ro_op
               wr_start = today
               wr_due = today
               wr_milestone = ro_milestone
               wr_qty_ord = 0
               wr_desc = ro_desc
               wr_wkctr = ro_wkctr
               wr_mch = ro_mch
               wr_mch_op = ro_mch_op
               wr_setup = ro_setup
               wr_run = ro_run
               wr_move = ro_move
               wr_tool = ro_tool
               wr_vend = ro_vend.
         end.

         assign
            wo_recno = recid(wo_mstr)
            wr_recno = recid(wr_route).

      end.

      find wr_route no-lock where recid(wr_route) = wr_recno.

      find ro_det where
             ro_routing = (if wo_routing > "" then wo_routing else wo_part)
         and ro_op = op
         and (ro_start = ? or ro_start <= today)
         and (ro_end = ? or ro_end >= today)
      no-lock no-error.

      if available ro_det then
         display ro_desc.
      else
         display wr_desc @ ro_desc.

      find pt_mstr where pt_part = wr_part no-lock no-error.
      if available pt_mstr then
         display pt_desc1.
      else
         display " " @ pt_desc1.

      status input.

      {gprun.i ""reoptr7b.p""}
/*GUI*/ if global-beam-me-up then undo, leave.


   end.
/*GUI*/ if global-beam-me-up then undo, leave.


   if control_total <> input_total then do:
      /* Batch control total does not match total */
      {pxmsg.i &MSGNUM=1151 &ERRORLEVEL=2}
   end.

end.
/*GUI*/ if global-beam-me-up then undo, leave.


status input.

if is_wiplottrace_enabled() then
   delete PROCEDURE h_wiplottrace_procs no-error.
if is_wiplottrace_enabled() then
   delete PROCEDURE h_wiplottrace_funcs no-error.

PROCEDURE update_next_previous:
   define output parameter cancel like mfc_logical initial yes no-undo.

   update
      part
      op
      prline
   with frame b
   editing:

      if frame-field = "part" then do:

         /* FIND NEXT/PREVIOUS RECORD */
         {mfnp.i pt_mstr part pt_part part pt_part pt_part}

         if recno <> ? then do:

            display
               pt_part @ part
               pt_desc1
            with frame b.

            find ptp_det where ptp_part = pt_part
                           and ptp_site = site
            no-lock no-error.

            assign
               routing = ""
               bom_code = "".

            if available ptp_det then
               assign
                  routing = ptp_routing
                  bom_code = ptp_bom_code.
            else
               assign
                  routing = pt_routing
                  bom_code = pt_bom_code.

            find first ro_det where
               ro_routing = if routing > "" then routing else pt_part
            no-lock no-error.

            if available ro_det then
               display ro_op @ op ro_desc with frame b.
            else
               display " " @ op " " @ ro_desc with frame b.

            display
               "" @ prline
               "" @ ln_desc
            with frame b.

            for each lnd_det where
                  lnd_site = site and lnd_part = pt_part
                  and (lnd_start <= last_date or lnd_start = ?)
            no-lock with frame b:

               find ln_mstr where ln_line = lnd_line
                              and ln_site = lnd_site
               no-lock no-error.

               if available ln_mstr then
                  display ln_line @ prline ln_desc with frame b.
               else
                  display "" @ prline "" @ ln_desc with frame b.

               leave.

            end.

            display
               routing
               bom_code
            with frame b.

         end.

         recno = ?.

      end.

      else
      if frame-field = "op"
         and can-find (pt_mstr where pt_part = input part)
      then do:

         find pt_mstr where pt_part = input part no-lock no-error.
         find ptp_det where ptp_part = pt_part
                        and ptp_site = site
         no-lock no-error.

         /* FIND NEXT/PREVIOUS RECORD */
         {mfnp01.i ro_det op ro_op
            "if available ptp_det
              then
              if ptp_routing > """" then ptp_routing else ptp_part
              else
              if pt_routing > """" then pt_routing else pt_part"
            ro_routing ro_routing}

         if recno <> ? then do:

            display
               ro_op @ op
               ro_desc
            with frame b.

            display
               "" @ prline
               "" @ ln_desc
            with frame b.

            for each lnd_det no-lock where
                     lnd_site = site and lnd_part = pt_part
                and (lnd_start <= last_date or lnd_start = ?)
            with frame b:

               find ln_mstr where ln_line = lnd_line
                              and ln_site = lnd_site
               no-lock no-error.

               if available ln_mstr then
                  display ln_line @ prline ln_desc with frame b.
               else
                  display "" @ prline "" @ ln_desc with frame b.

               leave.

            end.

         end.

      end.

      else if frame-field = "prline" then do:

         /* FIND/NEXT PREVIOUS*/
         {mfnp05.i lnd_det lnd_line
            "lnd_site = site and lnd_part = input part"
            lnd_line "input prline"}

         if recno <> ? then do:

            find ln_mstr where ln_line = lnd_line
                           and ln_site= lnd_site
            no-lock no-error.

            display
               lnd_line @ prline
            with frame b.

            if available ln_mstr then
               display ln_desc with frame b.
            else
               display "" @ ln_desc with frame b.

         end.

      end.

      else do:
         readkey.
         apply lastkey.
      end.

   end.

   cancel = no.

END PROCEDURE.
